<?

$_menu = D_MENU;

if ($d = (int)param('delete')) {
	
	dataset('db_delete', tb('data'), "id_parent=$d AND id_data=$_menu");
	dataset('db_delete', tb('data'), "id=$d AND id_data=$_menu");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
	exit;
}



switch (action(array('create_object'))) {


	case 'create_object':
		
		if ($title = trim(postdata('title'))) {

			$max = ($m = datafetch('db_item', tb('data'), "id_data=$_menu", 'MAX(id_index) as m')) ? $m['m']+1 : 1;
				
			if ($id = datafetch('db_insert', tb('data'), array('id_data' => $_menu, 'name' => $title, 'id_parent' => 0, 'id_index' => $max))) {
				
	
				// redirect to edit page
				header("Location: edit-$id/");
				exit;
			}
				// !!! else error on insert - handle it
		}
		
		break;

}
	

$DATA['objects'] = datafetch_array('db_list', array('table' => tb('data'), 'where' => "id_data=$_menu AND id_parent=0", 'index' => 'id', 'order' => 'name'));


//debug($DATA);


?>